//
//  CNComitOrderTableViewController.m
//  BeeQuick_19
//
//  Created by 赵玉忠 on 16/9/23.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNComitOrderTableViewController.h"
#import "CNPayTypeModel.h"
#import "CNComitOrderTableViewCell.h"
#import "CNpreferentialTableViewCell.h"
#import "CNTicketTableViewCell.h"
#import "CNTwoSectionModel.h"
#import "CNTicketTableViewCell.h"
#import "CNThreeSectionFirstCell.h"
#import "CNThreeSectionLastTableViewCell.h"
#import "CNFourSectionLastTableViewCell.h"
#import "DSHTTPClient.h"
#import "CNDiscountCouponModel.h"
#import "CNPayViewController.h"
#import "CNGoodsModel.h"
@interface CNComitOrderTableViewController ()
@property (nonatomic,strong) NSArray *payTypeModelArray;
@property (nonatomic,strong) NSArray *headSectionArray;
@property (nonatomic,strong) NSArray *TwoSectionArray;
@property (nonatomic,strong) NSArray *ThreeSectionArray;
@property (nonatomic,assign) NSInteger youHuiQuanCount;
@property (nonatomic,weak)   UIView *windowView;
@property (nonatomic,copy) NSString *count;
@property (nonatomic,weak) UILabel *leftLabelTwo;

@property (nonatomic,copy) NSString *urlPath;
@end

static NSString *payTypeCellID = @"payTypeCellID";
static NSString *headSectionCellID = @"headSectionCellID";
static NSString *FirstSecendLastCellID = @"FirstSecendLastCellID";
static NSString *FirstSecendOneAndTwoCellID = @"FirstSecendOneAndTwoCellID";
static NSString *CNThreeSectionFirstCellID = @"CNThreeSectionFirstCellID";
static NSString *CNThreeSectionLastTableViewCellID = @"CNThreeSectionLastTableViewCellID";
static NSString *CNFourSectionLastTableViewCellID = @"CNFourSectionLastTableViewCellID";

@implementation CNComitOrderTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
        
    //请求优惠券数据
    [self wengDangData];
    [self loadData];
    
    
    self.navigationItem.title = @"提交订单";
    self.tableView.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:0.7];
//    self.tableView.rowHeight = 65;
    self.payTypeModelArray = [self loadDataWith:@"comitOrder.plist" ToModel:[CNPayTypeModel class]];
    self.TwoSectionArray = [self loadDataWith:@"Ticket.plist" ToModel:[CNTwoSectionModel class]];
    [self.tableView registerNib:[UINib nibWithNibName:@"CNComitOrderTableViewCell" bundle:nil]
         forCellReuseIdentifier:payTypeCellID];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:headSectionCellID];
    [self.tableView registerNib:[UINib nibWithNibName:@"CNpreferentialTableViewCell" bundle:nil]
         forCellReuseIdentifier:FirstSecendLastCellID];
    [self.tableView registerNib:[UINib nibWithNibName:@"CNTicketTableViewCell" bundle:nil]forCellReuseIdentifier:FirstSecendOneAndTwoCellID];
    [self.tableView registerNib:[UINib nibWithNibName:@"CNThreeSectionFirstCell" bundle:nil]forCellReuseIdentifier:CNThreeSectionFirstCellID];
    [self.tableView registerNib:[UINib nibWithNibName:@"CNThreeSectionLastTableViewCell" bundle:nil]forCellReuseIdentifier:CNThreeSectionLastTableViewCellID];
    [self.tableView registerNib:[UINib nibWithNibName:@"CNFourSectionLastTableViewCell" bundle:nil]forCellReuseIdentifier:CNFourSectionLastTableViewCellID];


 //设置组头文字
    self.headSectionArray = @[@"选择支付方式",@"优惠",@"鲜锋精选",@"费用明细"];
    
 //往窗口添加视图
//    [self loadPayButtonView];
    
    
    
}
#pragma mark - 移除窗口视图
-(void)viewWillDisappear:(BOOL)animated{
    [self.windowView removeFromSuperview];
}
-(void)viewWillAppear:(BOOL)animated{
    [self loadPayButtonView];
//    self.leftLabelTwo.text = self.count;
}
#pragma mark - 往窗口添加视图
-(void)loadPayButtonView{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *zongView = [UIView new];
    self.windowView = zongView;
    UIView *lineView = [UIView new];

    [window addSubview:zongView];
    [zongView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(window);
        make.height.mas_equalTo(60);
    }];
    UIView *leftView = [UIView new];
    leftView.backgroundColor = [UIColor whiteColor];
    UILabel *leftLabelOne = [UILabel new];
    leftLabelOne.text = @"应付金额:";
    leftLabelOne.textColor = [UIColor blackColor];
    
    UILabel *leftLabelTwo = [UILabel new];
    leftLabelTwo.text = self.count;
    self.leftLabelTwo = leftLabelTwo;
    
    leftLabelTwo.textColor = [UIColor redColor];

    [leftView addSubview:leftLabelOne];
    [leftView addSubview:leftLabelTwo];
    [leftLabelOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftView).offset(10);
        make.centerY.equalTo(leftView);
    }];
    [leftLabelTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftLabelOne.mas_right).offset(3);
        make.centerY.equalTo(leftView);
    }];
    
    UIButton *rightView =[UIButton new];
    [rightView addTarget:self action:@selector(pushToPay) forControlEvents:UIControlEventTouchUpInside];
    [rightView setTitle:@"确认付款" forState:UIControlStateNormal];
    [rightView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [zongView addSubview:leftView];
    [zongView addSubview:rightView];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(zongView);
        make.width.mas_equalTo(250);
    }];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(zongView);
        make.left.equalTo(leftView.mas_right);
    }];
    rightView.backgroundColor = [UIColor yellowColor];
    
    //添加线
    [zongView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(zongView);
        make.height.mas_equalTo(1);
    }];
    lineView.backgroundColor = [UIColor colorWithRed:222/255.0 green:222/255.0 blue:222/255.0 alpha:1];
    
}
#pragma mark - 点击支付按钮跳转到支付页面
-(void)pushToPay{
    
    CNPayViewController *vc = [CNPayViewController new];
    [self.navigationController pushViewController:vc animated:YES];
    vc.payTypeModelArray = self.payTypeModelArray;
}

-(void)loadData{
   NSArray *array = [[SQLiteManager sharedSQLiteManager]queryContact];
   self.ThreeSectionArray = array;
    NSLog(@"%ld",array.count);
    CGFloat count = 0.0;
    for (CNGoodsModel *model in self.ThreeSectionArray) {
        count += model.count * model.partner_price;
    }
//    cell.count = [NSString stringWithFormat:@"%.2f",count];
    self.count = [NSString stringWithFormat:@"￥：%.2f",count];

    
}
-(NSArray *)loadDataWith:(NSString *)plistPath ToModel:(Class)Model{
    NSArray *plistArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:plistPath ofType:nil]];
    NSArray *modelArray = [NSArray yy_modelArrayWithClass:Model json:plistArray];
    return modelArray;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    

    NSInteger sectionNumber = 0;
    if (section == 0) {
        sectionNumber = 5;
    }else if (section == 1){
        sectionNumber = 3;
    }else if (section == 2){
        sectionNumber = self.ThreeSectionArray.count + 2;
    }else if (section == 3){
        sectionNumber = 2;
    }
    return sectionNumber;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *commonCell ;
    if (indexPath.section == 0) {
        commonCell = [self loadFirstSectionCell:tableView andWithIndexPath:indexPath];
    }else if(indexPath.section == 1){
        commonCell = [self loadOneSectionCell:tableView andWithIndexPath:indexPath];
    }else if (indexPath.section == 2){
        commonCell = [self loadThreeSectionCell:tableView andWithIndexPath:indexPath];
    }else{
        commonCell = [self loadFoureSectionCell:tableView andWithIndexPath:indexPath];
    }
    return commonCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 3 && indexPath.row == 1) {
        return 130;
    }else if(indexPath.row == 0 && indexPath.section != 1) {
        return 40;
    }else{
        return 55;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 15;
}

#pragma mark -  加载第一个组
-(UITableViewCell *)loadFirstSectionCell:(UITableView *)tableView andWithIndexPath:(NSIndexPath *)indexPath{
        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:headSectionCellID forIndexPath:indexPath];
            cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
            cell.textLabel.textColor = [UIColor darkGrayColor];
            
            cell.textLabel.text = self.headSectionArray[indexPath.row];
            return cell;
        }else{
            CNComitOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:payTypeCellID forIndexPath:indexPath];
            cell.indexPath = indexPath;
            cell.modelArray = self.payTypeModelArray;
            cell.block = ^{
                [self.tableView reloadData];
            };
            cell.model = self.payTypeModelArray[indexPath.row-1];
            if (indexPath.row == 3) {
                [cell loadJpgImage:@"kuaiqian.jpg"];
            }
            return cell;
        }
}
#pragma mark -  加载第二个组
-(UITableViewCell *)loadOneSectionCell:(UITableView *)tableView andWithIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        CNpreferentialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstSecendLastCellID forIndexPath:indexPath];
        
        return cell;
    }else if(indexPath.row == 0){
        CNTicketTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstSecendOneAndTwoCellID forIndexPath:indexPath];
//        NSLog(@"-----%ld",self.youHuiQuanCount);
//        cell.numberNew = [NSString stringWithFormat:@"%ld张可用优惠券",self.youHuiQuanCount];
        
        if (self.youHuiQuanCount == 0) {
            cell.numberNew = nil;
        }else{
            cell.numberNew = [NSString stringWithFormat:@"%ld张可用优惠券",self.youHuiQuanCount];
        }
        
        
        cell.model = self.TwoSectionArray[indexPath.row];
        
        return cell;

    }else{
        CNTicketTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstSecendOneAndTwoCellID forIndexPath:indexPath];
        cell.model = self.TwoSectionArray[indexPath.row];
        return cell;

    }
}
#pragma mark -  加载第三个组
-(UITableViewCell *)loadThreeSectionCell:(UITableView *)tableView andWithIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:headSectionCellID forIndexPath:indexPath];
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
        cell.textLabel.textColor = [UIColor darkGrayColor];
        
        cell.textLabel.text = self.headSectionArray[2];
        return cell;

    }else if (indexPath.row == self.ThreeSectionArray.count + 1){
        CNThreeSectionLastTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CNThreeSectionLastTableViewCellID forIndexPath:indexPath];
        CGFloat count = 0.0;
        for (CNGoodsModel *model in self.ThreeSectionArray) {
            count += model.count * model.partner_price;
        }
        cell.count = [NSString stringWithFormat:@"%.2f",count];
        self.count = cell.count;
        return cell;
    }else {
        CNThreeSectionFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:CNThreeSectionFirstCellID forIndexPath:indexPath];
        cell.model = self.ThreeSectionArray[indexPath.row-1];
        return cell;
    }
}
#pragma mark -  加载第四个组
-(UITableViewCell *)loadFoureSectionCell:(UITableView *)tableView andWithIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:headSectionCellID forIndexPath:indexPath];
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
        cell.textLabel.textColor = [UIColor darkGrayColor];
        
        cell.textLabel.text = self.headSectionArray[3];
        return cell;

    }else{
        CNFourSectionLastTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CNFourSectionLastTableViewCellID forIndexPath:indexPath];
        
#pragma mark - 设置tableView的滚动范围
        CGFloat height = CGRectGetMaxY(cell.frame);
        self.tableView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, height + 75);
        cell.amount = self.count;
//        self.leftLabelTwo.text = self.count;
        return cell;

    }
}
#pragma mark - 获取我的优惠数据
//文档
-(void)wengDangData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:@"9" forKey:@"call"];
    
    
    [DSHTTPClient postUrlString:@"http://iosapi.itcast.cn/loveBeen/MyCoupon.json.php" withParam:param withSuccessBlock:^(id data) {
        NSLog(@"%@",data);
        
        //获取优惠券数量
        NSArray *dataArray = data[@"data"];
        
        NSArray *youHuiQuanModelArray = [NSArray yy_modelArrayWithClass:[CNDiscountCouponModel class] json:dataArray];
        NSLog(@"%@",youHuiQuanModelArray);
        
        //判断优惠券数量
        NSInteger count = 0;
        for (CNDiscountCouponModel *model in youHuiQuanModelArray) {
            if (model.is_userd == 1) {
                count++;
            }
        }
        
        self.youHuiQuanCount = count;
        NSLog(@"%lu",(unsigned long)self.youHuiQuanCount);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"已经回到主线程，要做的事情");
            [self.tableView reloadData];
        });

        
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
    
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSIndexPath *indexPath =  [self.tableView indexPathForRowAtPoint:CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y)];
    UIView *headerView = [self.tableView headerViewForSection:indexPath.section] ;
    headerView.hidden = YES;
}
@end


